home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / unzip42.zip / OS2.ZIP / DOSNAME.C < prev    next >
C/C++ Source or Header  |  1992-03-18  |  4KB  |  158 lines

  1. /* Unix/HPFS filename translation for FAT file systems */
  2. /*  (with special unzip modifications:  sflag) */
  3.  
  4. /* Author: Kai Uwe Rommel */
  5.  
  6. #include "unzip.h"
  7.  
  8.  
  9. extern int sflag;  /* user wants to allow blanks (e.g., "EA DATA. SF") */
  10.  
  11. void ChangeNameForFAT(char *name)
  12. {
  13.   char *src, *dst, *next, *ptr, *dot, *start;
  14.   static char invalid[] = ":;,=+\"[]<>| \t";
  15.  
  16.   if ( isalpha(name[0]) && (name[1] == ':') )
  17.     start = name + 2;
  18.   else
  19.     start = name;
  20.  
  21.   src = dst = start;
  22.   if ( (*src == '/') || (*src == '\\') )
  23.     src++, dst++;
  24.  
  25.   while ( *src )
  26.   {
  27.     for ( next = src; *next && (*next != '/') && (*next != '\\'); next++ );
  28.  
  29.     for ( ptr = src, dot = NULL; ptr < next; ptr++ )
  30.       if ( *ptr == '.' )
  31.       {
  32.         dot = ptr; /* remember last dot */
  33.         *ptr = '_';
  34.       }
  35.  
  36.     if ( dot == NULL )
  37.       for ( ptr = src; ptr < next; ptr++ )
  38.         if ( *ptr == '_' )
  39.           dot = ptr; /* remember last _ as if it were a dot */
  40.  
  41.     if ( dot && (dot > src) &&
  42.          ((next - dot <= 4) ||
  43.           ((next - src > 8) && (dot - src > 3))) )
  44.     {
  45.       if ( dot )
  46.         *dot = '.';
  47.  
  48.       for ( ptr = src; (ptr < dot) && ((ptr - src) < 8); ptr++ )
  49.         *dst++ = *ptr;
  50.  
  51.       for ( ptr = dot; (ptr < next) && ((ptr - dot) < 4); ptr++ )
  52.         *dst++ = *ptr;
  53.     }
  54.     else
  55.     {
  56.       if ( dot && (next - src == 1) )
  57.         *dot = '.';           /* special case: "." as a path component */
  58.  
  59.       for ( ptr = src; (ptr < next) && ((ptr - src) < 8); ptr++ )
  60.         *dst++ = *ptr;
  61.     }
  62.  
  63.     *dst++ = *next; /* either '/' or 0 */
  64.  
  65.     if ( *next )
  66.     {
  67.       src = next + 1;
  68.  
  69.       if ( *src == 0 ) /* handle trailing '/' on dirs ! */
  70.         *dst = 0;
  71.     }
  72.     else
  73.       break;
  74.   }
  75.  
  76.   for ( src = start; *src != 0; ++src )
  77.     if ( (strchr(invalid, *src) != NULL) ||
  78.          ((*src == ' ') && !sflag) )  /* allow spaces if user wants */
  79.         *src = '_';
  80. }
  81.  
  82.  
  83. int IsFileNameValid(char *name)
  84. {
  85.   HFILE hf;
  86. #ifdef __32BIT__
  87.   ULONG uAction;
  88. #else
  89.   USHORT uAction;
  90. #endif
  91.  
  92.   switch( DosOpen(name, &hf, &uAction, 0, 0, FILE_OPEN,
  93.                   OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0) )
  94.   {
  95.   case ERROR_INVALID_NAME:
  96.   case ERROR_FILENAME_EXCED_RANGE:
  97.     return FALSE;
  98.   case NO_ERROR:
  99.     DosClose(hf);
  100.   default:
  101.     return TRUE;
  102.   }
  103. }
  104.  
  105.  
  106. typedef struct
  107. {
  108.   ULONG cbList;               /* length of value + 22 */
  109. #ifdef __32BIT__
  110.   ULONG oNext;
  111. #endif
  112.   BYTE fEA;                   /* 0 */
  113.   BYTE cbName;                /* length of ".LONGNAME" = 9 */
  114.   USHORT cbValue;             /* length of value + 4 */
  115.   BYTE szName[10];            /* ".LONGNAME" */
  116.   USHORT eaType;              /* 0xFFFD for length-preceded ASCII */
  117.   USHORT eaSize;              /* length of value */
  118.   BYTE szValue[CCHMAXPATH];
  119. }
  120. FEALST;
  121.  
  122.  
  123. int SetLongNameEA(char *name, char *longname)
  124. {
  125.   EAOP eaop;
  126.   FEALST fealst;
  127.  
  128. #ifndef __32BIT__
  129.   if ( _osmode == DOS_MODE )
  130.     return 0;
  131. #endif
  132.  
  133.   eaop.fpFEAList = (PFEALIST) &fealst;
  134.   eaop.fpGEAList = NULL;
  135.   eaop.oError = 0;
  136.  
  137.   strcpy(fealst.szName, ".LONGNAME");
  138.   strcpy(fealst.szValue, longname);
  139.  
  140.   fealst.cbList  = sizeof(fealst) - CCHMAXPATH + strlen(fealst.szValue);
  141.   fealst.cbName  = (BYTE) strlen(fealst.szName);
  142.   fealst.cbValue = sizeof(USHORT) * 2 + strlen(fealst.szValue);
  143.  
  144. #ifdef __32BIT__
  145.   fealst.oNext   = 0;
  146. #endif
  147.   fealst.fEA     = 0;
  148.   fealst.eaType  = 0xFFFD;
  149.   fealst.eaSize  = strlen(fealst.szValue);
  150.  
  151.   return DosSetPathInfo(name, FIL_QUERYEASIZE,
  152.                         (PBYTE) &eaop, sizeof(eaop), 0
  153. #ifndef __32BIT__
  154.                        , 0
  155. #endif
  156.                        );
  157. }
  158.